package org.zoomdev.zoom.web.action;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.zoomdev.zoom.aop.Invoker;
import org.zoomdev.zoom.web.parameter.ParameterParser;
import org.zoomdev.zoom.web.parameter.PreParameterParser;
import org.zoomdev.zoom.web.rendering.Rendering;

import java.lang.reflect.Method;

public final class MethodActionHandler extends ActionHandler {

    // 对应的controller class
    public final Class controller;

    // 方法
    public final Method method;

    private final MethodAction action;

    public Invoker invoker;
    /**
     * 渲染模版后缀
     */
    public String ext;

    // 方法参数名称
    public String[] parameterNames;

    /**
     * 运行时初始化
     */
    public Rendering rendering;
    public ParameterParser parameterParser;
    public PreParameterParser preParamParser;
    public String encoding;
    public Object instance;

    public BeforeInvokeInterceptor[] beforeInvokeInterceptors;

    public PreParseInterceptor[] preInterceptors;
    public ParseInterceptor[] parseInterceptors;
    public ResultInterceptor[] resultInterceptors;
    public ErrorInterceptor[] errorInterceptors;
    public CompleteInterceptor[] completeInterceptors;

    public MethodActionHandler(String key,
                               String[] methods,
                               String[] pathVariableNames,
                               MethodAction action,
                               Class controller,
                               Method method) {
        super(key, methods, pathVariableNames);
        this.action = action;
        this.controller = controller;
        this.method = method;
    }

    @Override
    public String toString() {
        return "action:" + method;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        action.handle(new ActionContext(request, response, this));
    }
}
